﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;

namespace Xyzzer.AsyncUI
{
    public sealed partial class EventDrivenCustomDialog
    {
        public EventDrivenCustomDialog()
        {
            InitializeComponent();
        }

        private FrameworkElement RootFrameworkElement
        {
            get
            {
                return (FrameworkElement)Window.Current.Content;
            }
        }

        /// <summary>
        /// Flag for preventing reentrancy in the Show() method.
        /// </summary>
        private bool shown;
        List<ButtonBase> buttons = new List<ButtonBase>();

        public class TextEventArgs : EventArgs
        {
            public string Text{ get; private set; }

            internal TextEventArgs (string text)
	        {
                this.Text = text;
	        }
        }

        public event EventHandler<TextEventArgs> DialogDismissed;

        public void Show(string title, string text, params string[] buttonTexts)
        {
            if (shown)
            {
                throw new InvalidOperationException();
            }

            shown = true;
            this.TitleTextBlock.Text = title;
            this.TextTextBlock.Text = text;

            foreach (var buttonText in buttonTexts)
            {
                var button = (ButtonBase)CloseButtonTemplate.LoadContent();
                button.Content = buttonText;
                button.Click += button_Click;
                buttons.Add(button);
                ButtonsPanel.Children.Add(button);
            }
            
            ResizeLayoutRoot();

            // Show dialog
            this.DialogPopup.IsOpen = true;
            this.SlideInAnimation.From =
                this.ContentBorderTranslation.X = RootFrameworkElement.ActualWidth;
            this.SlideInStoryboard.Begin();
        }

        private ButtonBase lastClickedButton;
        private void button_Click(object sender, RoutedEventArgs e)
        {
            foreach (var button in buttons)
            {
                button.Click -= button_Click;
            }

            lastClickedButton = (ButtonBase)sender;

            // Hide dialog
            this.SlideOutAnimation.To = -RootFrameworkElement.ActualWidth;
            this.SlideOutStoryboard.Begin();
        }

        private void SlideOutStoryboard_Completed(object sender, object e)
        {
            this.DialogPopup.IsOpen = false;
            ButtonsPanel.Children.Clear();
            shown = false;

            if (this.DialogDismissed != null)
            {
                this.DialogDismissed(
                    this,
                    new TextEventArgs((string)lastClickedButton.Content));
            }
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            ResizeLayoutRoot();
            return base.ArrangeOverride(finalSize);
        }

        private void ResizeLayoutRoot()
        {
            var root = RootFrameworkElement;
            LayoutRoot.Width = root.ActualWidth;
            LayoutRoot.Height = root.ActualHeight;
        }
    }
}
